package com.suyuan.controller.shop.admin;

import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.ProducetypeInfo;
import com.suyuan.entity.ProductInfo;
import com.suyuan.entity.PurchaseInfo;
import com.suyuan.service.*;
import com.suyuan.utils.PictureUtils;
import com.suyuan.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import com.suyuan.entity.*;
import com.suyuan.service.CompanyService;
import com.suyuan.service.ProducetypeService;
import com.suyuan.service.ProductService;
import com.suyuan.service.PurchaseService;
import com.suyuan.utils.JsonMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/shop/admin/product")
public class Product2Controller {

    @Resource(name = "productService")
    private ProductService productService;
    @Resource(name = "purchaseService")
    private PurchaseService purchaseService;
    @Resource(name = "companyService")
    private CompanyService companyService;
    @Resource(name = "producetypeService")
    private ProducetypeService producetypeService;
    @Resource(name = "pictureService")
    private PictureService pictureService;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(ModelMap model){
        List<ProductInfo> products = productService.findAll();
        model.addAttribute("products",products);
        return "product_list";
    }

    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        //公司集合
        List<CompanyInfo> companys = companyService.findByCategory(103);//103为养殖企业

        //产品类型集合
        List<ProducetypeInfo> types = producetypeService.findAll();
        //生猪集合
        List<PurchaseInfo> animals = purchaseService.findAll();
        model.addAttribute("companys",companys);
        model.addAttribute("types",types);
        model.addAttribute("animals",animals);
        return "product_add";
    }

    /**
     * 保存
     * @param product
     * @return
     */
    @RequestMapping(value = "save")
    public String save(ProductInfo product, HttpServletRequest request){
        product.setSellprice(product.getProcprice());
        product.setTracecode(new Date().getTime()+"");
        String littlePicLocation = null;

        if (request instanceof MultipartHttpServletRequest) {
            System.out.println("222");
            //此处先保存商品缩略图先
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            // 获取上传的文件集合
            Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
            for(Map.Entry<String, MultipartFile> entry : fileMap.entrySet()){
                // 对文件进处理
                System.out.println(entry.getKey() + ":" + entry.getValue().getOriginalFilename());
                if("littlePic".equals(entry.getKey())){
                    //缩略图
                    try {
                        littlePicLocation = pictureService.saveLittlePic(entry.getValue(),request);
                        fileMap.entrySet().remove(entry.getValue());//删掉所旅途
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("缩略图保存失败");
                    }
                }
            }

            Integer productId = null;
            try {
                product.setImagesrc(littlePicLocation);
                productId = productService.insertProductReturnId(product);
                System.out.println("直接获取productId："+product.getProductid());
                productId = product.getProductid();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("商品保存失败");
            }

            //遍历全部滚动图和详情图并保存
            try {
                pictureService.savePic(fileMap,request,productId);
            } catch (Exception e) {
                System.out.println("滚动图和详情图保存失败");
                e.printStackTrace();
            }
        }
        return "redirect:/shop/admin/product/list";
    }

    @RequestMapping(value = "edit")
    public String edit(ModelMap model , Integer id){
        //公司集合
        List<CompanyInfo> companys = companyService.findAll();
        //产品类型集合
        List<ProducetypeInfo> types = producetypeService.findAll();
        //生猪集合
        List<PurchaseInfo> animals = purchaseService.findAll();
        //商品信息
        ProductVO product = productService.findById(id);

        model.addAttribute("companys",companys);
        model.addAttribute("types",types);
        model.addAttribute("animals",animals);
        model.addAttribute("productVO",product);
        return "product_edit";
    }

    /**
     * 更新
     * @param product
     * @return
     */
    @RequestMapping(value = "update")
    public String update(ProductInfo product){
        productService.update(product);
        return "redirect:/shop/admin/product/list";
    }

    @RequestMapping(value = "/delete")
    public String delete(Integer id){
        productService.delete(id);
        return "redirect:/shop/admin/product/list";
    }


    /**
     * 溯源商城后台添加商品
     * @param id
     * @return
     */
    @RequestMapping("addProduct")
    public String addRecommProduct(Integer id){
        productService.addRecommProduct(id);
        return "redirect:/shop/admin/product/list2";
    }

    /**
     * 溯源商城展示出热门商品
     * @param modelMap
     * @return
     */
    @RequestMapping("list2")
    public String listRecommProduct(ModelMap modelMap){
        List<RecommProduct> recommProductList = productService.findAllRecommProduct();
        modelMap.addAttribute(recommProductList);
        return "recommProduct_list";
    }


    /**
     * 移除热门商品
     * @param id
     * @return
     */
    @RequestMapping("remove")
    public String  removeRecommProduct(Integer id){
        productService.removeRecommProduct(id);
        return "redirect:/shop/admin/product/list2";
    }


    @RequestMapping("check")
    @ResponseBody
    public Object checkInfo(Integer id){
        Integer flag = productService.checkInfo(id);
        if(flag == 1){
            return JsonMessage.error("商品编号错误");
        }else if(flag == 2){
            return JsonMessage.error("热门商品数量已达上限");
        }else if (flag == 3){
            return JsonMessage.success("热门商品可用");
        }
        return null;
    }

}
